package pl.wroc.uni.ii.puzzle.activity;

import java.util.LinkedList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import pl.wroc.uni.ii.puzzle.R;
import pl.wroc.uni.ii.puzzle.data.DatabaseAdapter;
import pl.wroc.uni.ii.puzzle.data.Score;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.SQLException;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class StatsActivity extends Activity {

	private GraphicalView chartView;
	private LinearLayout layout;
	private ProgressDialog progressDialog;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		showProgressDialog();
		new ScoresLookupTask().execute();
	}
	
	private void showProgressDialog() {
		String loading = getString(R.string.loading);
		String receivingData = getString(R.string.receivingData);
		boolean indeterminate = true;
		boolean cancelable = true;
		progressDialog = ProgressDialog.show(this, loading, receivingData, indeterminate, cancelable);
	}

	private void fillChartWithScoreList(List<Score> scores) {
		chartView = ChartFactory.getTimeChartView(this, createDateDataset(scores),
				setChartSettings(getDemoRenderer()), null);
		layout = new LinearLayout(this);
		layout.addView(chartView, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		setContentView(layout);
	}
	
	private XYMultipleSeriesDataset createDateDataset(List<Score> scores) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		TimeSeries series = new TimeSeries("series 1");
		int MILIS_PER_SECOND = 1000;
		for (Score score : scores) {
			series.add(score.getDate(), score.getScore()/MILIS_PER_SECOND);
		}
		dataset.addSeries(series);

		
		TimeSeries series2 = new TimeSeries("");
		dataset.addSeries(series2);
			
		return dataset;
	}


	private XYMultipleSeriesRenderer getDemoRenderer() {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		renderer.setPointSize(5f);
		renderer.setMargins(new int[] { 20, 30, 15, 0 });
		XYSeriesRenderer r = new XYSeriesRenderer();
		r.setColor(Color.BLUE);
		r.setPointStyle(PointStyle.SQUARE);
		r.setFillBelowLine(true);
		r.setFillBelowLineColor(Color.WHITE);
		r.setFillPoints(true);
		renderer.addSeriesRenderer(r);
		r = new XYSeriesRenderer();
		renderer.addSeriesRenderer(r);
		renderer.setAxesColor(Color.DKGRAY);
		renderer.setLabelsColor(Color.LTGRAY);
		return renderer;
	}

	private XYMultipleSeriesRenderer setChartSettings(XYMultipleSeriesRenderer renderer) {
		renderer.setChartTitle(getString(R.string.scoresChart));
		renderer.setXTitle(getString(R.string.time));
		renderer.setYTitle(getString(R.string.scores));
		return renderer;
	}
	
	public List<Score> retrieveMyScores() {
		DatabaseAdapter databaseAdapter = new DatabaseAdapter(this);
		List<Score> scores = null;
		try {
			databaseAdapter.open();
			scores = databaseAdapter.getHighScores();
			databaseAdapter.close();
		} catch (SQLException e) {
			Log.e(HighScoresActivity.class.getName(), "can't receive data from base", e);
			showToast(getString(R.string.canNotReceiveDataFromBase));
		}
		return (scores != null) ? scores : new LinkedList<Score>();
	}

	private void showToast(String message) {
		Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
	}
	
	private class ScoresLookupTask extends AsyncTask <Void, List<Score>, List<Score>> {
		
		@Override
		protected List<Score> doInBackground(Void... params) {
			return retrieveMyScores();
		}

		@Override
		protected void onPostExecute(List<Score> scores) {
			super.onPostExecute(scores);
			fillChartWithScoreList(scores);
			progressDialog.dismiss();
		}
	}
}
